﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：BaseModule.cs
// 功能描述：TongTongMall
// 
// Create User：jym 2017/02/09
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TongTongMall.Configuration.Startup;
using TongTongMall.Dependecy;

namespace TongTongMall.Modules
{
    public abstract class BaseModule
    {
        protected internal IIocManager IocManager { get; internal set; }


        /// <summary>
        /// 系统内相关配置信息.
        /// </summary>
        protected internal IStartupConfiguration Configuration { get; internal set; }


         


        public virtual void PreInitialize()
        {

        }


        public virtual void Initialize()
        {

        }


        public virtual void PostInitialize()
        {

        }


        public virtual void Shutdown()
        {

        }

        public static bool IsBaseModule(Type type)
        {
          
            
            return
                type.IsClass &&
                !type.IsAbstract &&
                typeof(BaseModule).IsAssignableFrom(type);
           
        }

   
        /// <summary>
        /// 找到所有与改模块相关的所有依赖模块
        /// </summary>
        /// <param name="moduleType"></param>
        /// <returns></returns>
        public static List<Type> FindDependedModuleTypes(Type moduleType)
        {
            if (!IsBaseModule(moduleType))
            {
                throw new TongTongMallException("This type is not an BaseModule module: " + moduleType.AssemblyQualifiedName);
            }

            var list = new List<Type>();

            if (moduleType.IsDefined(typeof(DependsOnAttribute), true))
            {
                var dependsOnAttributes = moduleType.GetCustomAttributes(typeof(DependsOnAttribute), true).Cast<DependsOnAttribute>();
                foreach (var dependsOnAttribute in dependsOnAttributes)
                {
                    foreach (var dependedModuleType in dependsOnAttribute.DependedModuleTypes)
                    {
                        list.Add(dependedModuleType);
                    }
                }
            }

            return list;
        }
    }
}
